package com.jsgt.greekfire.ui.home.entrance.createwallet.checkhelpword

import android.app.Activity
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.jsgt.greekfire.*
import com.jsgt.greekfire.base.BaseBackgroundActivity
import com.jsgt.greekfire.common.util.YTWalletUtils
import com.jsgt.greekfire.ui.home.entrance.createwallet.checkhelpword.adapter.WordCheckAdapter
import com.jsgt.greekfire.ui.mine.user.backupwalletword.adapter.WordHelperAdapter
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.WalletBean
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_check_help_word_2.*
import kotlin.collections.ArrayList

/**
 * 验证助记词2
 */
class CheckHelpWord2Activity : BaseBackgroundActivity() {

    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }
    private var identityBean: IdentityBean? = null

    private val wordHelperAdapter: WordHelperAdapter by lazy {
        WordHelperAdapter()
    }


    private val wordCheckAdapter by lazy { WordCheckAdapter() }
    private val checkWordList by lazy { arrayListOf<String>() }

    private var helpString = ArrayList<String>()
    private var words: ArrayList<String>? = null
    private var identityId = -1

    override fun getLayoutId(): Int = R.layout.activity_check_help_word_2

    override fun main() {
//        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)//防止截屏
//        title = getString(R.string.check_help_word)
        identityId = intent.getIntExtra("identityId", -1)
        identityBean = intent.getParcelableExtra("identity")
        if (identityId == -1 && identityBean == null) {
            finish()
        }

        wordHelperAdapter.itemClickListner = {
            if (wordCheckAdapter.dataList().size < words!!.size) {
                checkWordList.clear()
                checkWordList.add(it)
                wordCheckAdapter.addData(checkWordList)
                wordHelperAdapter.dataList().remove(it)
                wordHelperAdapter.notifyDataSetChanged()
                if (wordsIsSort()) {
                    help_word_error.visibility = View.GONE
                } else {
                    help_word_error.visibility = View.VISIBLE
                }
            }
        }
        wordCheckAdapter.viewClickListener = {
            wordHelperAdapter.dataList().add(it)
            wordHelperAdapter.notifyDataSetChanged()
            wordCheckAdapter.dataList().remove(it)
            wordCheckAdapter.notifyDataSetChanged()
            if (wordsIsSort()) {
                help_word_error.visibility = View.GONE
            } else {
                help_word_error.visibility = View.VISIBLE
            }
        }

        word_rv.adapter = wordHelperAdapter

        wordCheckAdapter.addData(checkWordList)
        check_word_rv.adapter = wordCheckAdapter

        confirm_tv.setOnClickListener {
            if (wordsIsEquals()) {
                identityBean?.let {
                    if (identityId != -1) {
                        val backup = IdentityBean.Backup(it.id, true)
                        walletViewModel.updateIdentityBackup(backup)
                    }
                    setResult(Activity.RESULT_OK)
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
                    finish()
                }
            }
        }

        if (identityBean == null) {
            walletViewModel.getIdentity(identityId)?.observe(this, Observer {
                if (it != null && words == null) {
                    identityBean = it
                    words = it.getMnemonicByAES().split(" ") as ArrayList<String>
                    helpString.clear()
                    helpString.addAll(words!!)
                    //打乱助记词顺序
                    helpString.shuffle()
                    wordHelperAdapter.addData(helpString)
                }
            })
        } else {
            words = identityBean!!.getMnemonicByAES().split(" ") as ArrayList<String>
            helpString.clear()
            helpString.addAll(words!!)
            //打乱助记词顺序
            helpString.shuffle()
            wordHelperAdapter.addData(helpString)
        }
    }

    /**
     *  比较选择的助记词是否相等
     */
    private fun wordsIsEquals(): Boolean {
        if (wordCheckAdapter.dataList().size == words?.size) {
            for ((index, word) in words!!.withIndex()) {
                if (!TextUtils.equals(word, wordCheckAdapter.dataList()[index])) {
                    return false
                }
            }
            return true
        } else {
            return false
        }
    }

    /**
     *  比较选择的助记词顺序是否相同
     */
    private fun wordsIsSort(): Boolean {
        if (wordCheckAdapter.dataList().isEmpty()) {
            return true
        }
        for (i in wordCheckAdapter.dataList().indices) {
            if (wordCheckAdapter.dataList()[i] != words?.get(i)) {
                return false
            }
        }
        return true
    }
}